import java.util.Stack;
//https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/
class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            String str = tokens[i];
            if (!isOperator(str)) {
                stack.push(Integer.parseInt(str));
            }else {
                int num1 = stack.pop();
                int num2 = stack.pop();
                switch (str) {
                    case "+":
                        stack.push(num2 + num1);
                        break;
                    case "-":
                        stack.push(num2 - num1);
                        break;
                    case "*":
                        stack.push(num2 * num1);
                        break;
                    case "/":
                        stack.push(num2 / num1);
                        break;
                }
            }
        }
        return stack.pop();
    }

    //'+'、'-'、'*' 和 '/' 。
    private boolean isOperator(String str) {
        if (str.equals("+") || str.equals("-") ||
                str.equals("*") || str.equals("/")) {
            return true;
        }
        return false;
    }
}